'use strict';
app.controller('benefitCtrl',['$scope','$modal','utilService','benefitService','toaster',
    function($scope,$modal,utilService,benefitService,toaster){

    $scope.benefit={};

    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'benefit',
            params:$scope.benefit
        }).then(function(result){
        });

    }

    $scope.query();


    $scope.del = function(id){
        var param = {
            id:id
        };

        // query data.
        openModal("你是否要删除该让利类别？");

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除让利类别",
                controller:"benefitDeleteCtrl",
                message:msg,
                data:param
            });
        }
    }
}]);
//增加
app.controller('benefitAddCtrl',['$scope','utilService','benefitService','toaster','$state',
    function($scope,utilService,benefitService,toaster,$state){
    $scope.benefit={};
    $scope.add=function(){
        benefitService.add($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.benefit');
                    break;
                //case 674:
                //    toaster.pop('waiting', "提示", result.message);
                //    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.findMax=function(){
        benefitService.findMax($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.benefit=result.data.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    };

    $scope.findMax();

}]);


//删除
app.controller('benefitDeleteCtrl',['$scope','$modalInstance','benefitService','toaster','modalData',
    function($scope,$modalInstance,benefitService,toaster,modalData){
        $scope.modal = modalData;
        $scope.isDelModal=true;
        $scope.benefitId=modalData.data.id;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            benefitService.delete($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        $scope.query();
                        toaster.pop('success', "提示", "操作成功");
                        break;
                    case 688:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
}]);

//修改
app.controller('benefitEditCtrl',['$scope','utilService','benefitService','toaster','$state','$stateParams',
    function($scope,utilService,benefitService,toaster,$state,$stateParams){
    $scope.benefitId=$stateParams.id;
    $scope.findById=function(){
        benefitService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.benefit=result.data.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    };

    $scope.findById();

    $scope.update=function(){
        benefitService.update($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.benefit');
                    break;
                case 688:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
}]);
